package checkers3d.storage;

import checkers3d.logic.*;
import java.io.*;
import java.util.*;

/**
 *
 * @author Sean Keel
 * @author Ruben Acuna
 */
public class DataManagerUtil {

    static public void createFile(String filename) throws IOException{
        File reader = new File(filename);
        reader.createNewFile();
    }

    static public void eraseFile(String filename) throws IOException{
        FileWriter formattedFile = new FileWriter(filename, false);
        formattedFile.close();
    }

    static public void writePlayingSurface3D(BufferedWriter out, PlayingSurface3D playingSurface3D) throws IOException {
        out.write("P\t");
        out.newLine();
        out.write(playingSurface3D.getSizeX() + "\t"
                + playingSurface3D.getSizeY() + "\t"
                + playingSurface3D.getSizeZ());
        out.newLine();
        
        for (int x = 0; x < playingSurface3D.getSizeX(); x++) {
            for (int y = 0; y < playingSurface3D.getSizeY(); y++) {
                for (int z = 0; z < playingSurface3D.getSizeZ(); z++) {                    
                    String object = "*\t";

                    for(IBoardObject piece : playingSurface3D.getPositionObjects(new Point3D(x, y, z)))
                        object = object + piece.getDataAsString() + "\t";

                    out.write(object);
                    out.newLine();
                }
            }
        }
    }

    static public PlayingSurface3D readPlayingSurface3D(Scanner scan, Player home, Player visitor){
        String tokens[];
        String garbage = new String();
        String temp = new String();

        garbage = scan.nextLine();
        System.out.println("\nUTIL GARBAGE: " + garbage);
        garbage = scan.nextLine();
        System.out.println("\nUTIL GARBAGE: " + garbage);
        int boardSizeX = scan.nextInt();
        int boardSizeY = scan.nextInt();
        int boardSizeZ = scan.nextInt();

        //RA: get rid of spare return character left over from nextInt()s.
        garbage = scan.nextLine();

        PlayingSurface3D ps3D = new PlayingSurface3D(boardSizeX, boardSizeY, boardSizeZ);

        for (int x = 0; x < boardSizeX; x++) {
            for (int y = 0; y < boardSizeY; y++) {
                for (int z = 0; z < boardSizeZ; z++) {

                    // Get the next line and split contents into
                    // individual strings.
                    temp = scan.nextLine();
                    tokens = temp.split("\t");

                    // Otherwise we need to get the board objects.
                    if (tokens[0].equals("*") && tokens.length >= 0){
                        for (int i=0; i < tokens.length; i++){
                            if (i % 2 == 1){
                                IBoardObject object = null;

                                if (tokens[i].equals("Mine")){
                                    if (tokens[i+1].equals(home.getName()))
                                        object = new BoardObjectMine(home, home.getMineRR());
                                    else
                                        object = new BoardObjectMine(visitor, visitor.getMineRR());
                                }
                                if (tokens[i].equals("Safe")){
                                    if (tokens[i+1].equals(home.getName()))
                                        object = new BoardObjectSafe(home, home.getSafeRR());
                                    else
                                        object = new BoardObjectSafe(visitor, visitor.getSafeRR());
                                }
                                if (tokens[i].equals("Wall")){
                                    if (tokens[i+1].equals(home.getName()))
                                        object = new BoardObjectWall(home, home.getWallRR());
                                    else
                                        object = new BoardObjectWall(visitor, visitor.getWallRR());
                                }
                                if (tokens[i].equals("Checker")){
                                    if (tokens[i+1].equals(home.getName()))
                                        object = new BoardObjectChecker(home, home.getCheckerRR());
                                    else
                                        object = new BoardObjectChecker(visitor, visitor.getCheckerRR());
                                }
                                if (tokens[i].equals("King")){
                                    if (tokens[i+1].equals(home.getName()))
                                        object = new BoardObjectKing(home, home.getKingRR());
                                    else
                                        object = new BoardObjectKing(visitor, visitor.getKingRR());
                                }

                                if(object != null)
                                    ps3D.addObject(object, new Point3D(x, y, z));
                            }
                        }
                    }
                }
            }
        }

        return ps3D;
    }

    /**
     * Returns an array with the names of all availiable files.
     *
     * @return Array of file names.
     */
    public static String[] getFilenames(String mount, String extension)
    {
        LinkedList<String> names = new LinkedList<String>();

        File folder = new File(mount);

        //folder is null if it is empty
        if(folder.listFiles() != null)
        {
            for(File file : folder.listFiles())
                if(file.isFile())
                {
                    String filename = file.getName();

                    //if a filename within the selected folder ends with the
                    //given extension, we assume it is a valid scenario.
                    if(filename.toLowerCase().endsWith(extension))
                        //the name of a file is simply it's filename minus the
                        //file extension.
                        names.add(filename.substring(0, filename.length() - 4));
                }
        }

        return names.toArray(new String[names.size()]);
    }
}